package ui;

import control.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created with IntelliJ IDEA.
 * User: Deshun
 * Date: 2/14/13
 * Time: 10:30 AM
 * To change this template use File | Settings | File Templates.
 */
public class ItemToolbar {

    private JLabel notificationLabel;

    private JButton textField;
    private JButton multiTextField;
    private JButton datePicker;
    private JButton timePicker;
    private JButton radio;
    private JButton checkbox;
    private JButton dropdownList;
    private JButton listBox;
    private JButton matrix;
    private JButton fileAttachment;
    private JButton image;
    private JButton divisionLine;
    private JButton pageBreak;

    private int rowNumber;
    private FormArea formArea;

    public ItemToolbar() {

    }

    public ItemToolbar(FormArea formArea, int rowNumber) {
        this.rowNumber = rowNumber;
        this.formArea = formArea;
    }

    //TODO redo layout
    //TODO link up action handlers for all buttons
    public Component itemToolbarPanel() {
        JPanel panel = new JPanel();
        panel.setOpaque(false);

        notificationLabel = new JLabel("Add item:");

        textField = new JButton("T");
        multiTextField = new JButton("M");
        datePicker = new JButton("D");
        timePicker = new JButton("T");
        radio = new JButton("R");
        checkbox = new JButton("C");
        dropdownList = new JButton("D");
        listBox = new JButton("L");
        matrix = new JButton("M");
        fileAttachment = new JButton();
        image = new JButton();
        divisionLine = new JButton();
        pageBreak = new JButton();


        textField.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent evt) {
                formArea.addControl(rowNumber, new Textfield());
            }
        });

        multiTextField.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                formArea.addControl(rowNumber, new MultiLineTextfield());
            }
        });

        datePicker.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                formArea.addControl(rowNumber, new DatePicker());
            }
        });

        timePicker.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                formArea.addControl(rowNumber, new TimePicker());
            }
        });

        radio.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                formArea.addControl(rowNumber, new Radio());
            }
        });

        checkbox.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                formArea.addControl(rowNumber, new CheckBox());
            }
        });

        dropdownList.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                formArea.addControl(rowNumber, new Dropdown());

            }
        });

        listBox.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                formArea.addControl(rowNumber, new Listbox());
            }
        });

        matrix.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                formArea.addControl(rowNumber, new Matrix());
            }
        });

        panel.setLayout(new FlowLayout(FlowLayout.LEADING));

        Dimension size = new Dimension(35, 35);
        textField.setPreferredSize(size);
        multiTextField.setPreferredSize(size);
        datePicker.setPreferredSize(size);
        timePicker.setPreferredSize(size);
        radio.setPreferredSize(size);
        checkbox.setPreferredSize(size);
        dropdownList.setPreferredSize(size);
        listBox.setPreferredSize(size);
        matrix.setPreferredSize(size);
        fileAttachment.setPreferredSize(size);
        image.setPreferredSize(size);
        divisionLine.setPreferredSize(size);
        pageBreak.setPreferredSize(size);

        panel.add(textField);
        panel.add(datePicker);
        panel.add(timePicker);
        panel.add(radio);
        panel.add(checkbox);
        panel.add(dropdownList);
        panel.add(listBox);
        panel.add(matrix);
        panel.add(fileAttachment);
        panel.add(image);
        panel.add(divisionLine);
        panel.add(pageBreak);

/*      GroupLayout layout = new GroupLayout(panel);

        panel.setLayout(layout);

        layout.setHorizontalGroup(
                layout.createParallelGroup()
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup()
                                        .addComponent(notificationLabel)
                                        .addGroup(layout.createSequentialGroup()
                                                .addGap(3)
                                                .addComponent(textField, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(multiTextField, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(datePicker, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(timePicker, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(radio, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(checkbox, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(dropdownList, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(listBox, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(matrix, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(fileAttachment, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(image, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(divisionLine, 35, 35, 35)
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 5, 5)
                                                .addComponent(pageBreak, 35, 35, 35)
                                        )
                                )
                        )
        );


        layout.setVerticalGroup(
                layout.createSequentialGroup()
                        .addComponent(notificationLabel)
                        .addGap(5)
                        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(textField, 35, 35, 35)
                                .addComponent(multiTextField, 35, 35, 35)
                                .addComponent(datePicker, 35, 35, 35)
                                .addComponent(timePicker, 35, 35, 35)
                                .addComponent(radio, 35, 35, 35)
                                .addComponent(checkbox, 35, 35, 35)
                                .addComponent(dropdownList, 35, 35, 35)
                                .addComponent(listBox, 35, 35, 35)
                                .addComponent(matrix, 35, 35, 35)
                                .addComponent(fileAttachment, 35, 35, 35)
                                .addComponent(image, 35, 35, 35)
                                .addComponent(divisionLine, 35, 35, 35)
                                .addComponent(pageBreak, 35, 35, 35)
                        )
        );*/

        return panel;
    }

}
